package com.yuanheng100.ems4j.web.controller.session;

import com.yuanheng100.ems4j.api.IDepartmentService;
import com.yuanheng100.ems4j.model.Ems4jDepartment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 部门相关操作处理
 *
 * Created by wangguangshuo on 2017/1/19.
 */
@RestController
@RequestMapping("/dep")
public class DepartmentController
{
    private static Logger logger = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private IDepartmentService departmentService;


    /**
     * 获取部门树组织结构
     * @param response
     * @return
     */
    @RequestMapping("/getDepartmentStructure")
    public Ems4jDepartment getDepartmentStructure(HttpServletResponse response)
    {
        Ems4jDepartment department = departmentService.getDepartmentById(1);
        department.setSubDepartmentList(departmentService.getSubDepartmentsByParentId(1, 0, (short) 0, 1));
        return department;
    }

    /**
     * 添加部门
     * @param response
     * @param department
     * @return
     */
    @RequestMapping("/addDepartment")
    public Ems4jDepartment addDepartment(HttpServletResponse response, Ems4jDepartment department)
    {
        logger.info("收到的部门名称："+department.getName());
//        department.setName(new String(department.getName().getBytes("iso-8859-1"),"utf-8"));
//        logger.info("处理后部门名称："+department.getName());
        department.setDepstatus((short)1);
        int i = departmentService.addDepartment(department);
        department.setId(i);
        return department;
    }


    /**
     * 查询部门
     * @param response
     * @param depId 部门id
     * @return
     */
    @RequestMapping("/getDepartmentInfo")
    public Ems4jDepartment getDepartmentInfo(HttpServletResponse response,Integer depId)
    {
        Ems4jDepartment department = departmentService.getDepartmentById(depId);
        department.setParentDepartment(departmentService.getDepartmentById(department.getParentid()));
        return department;
    }


    /**
     * 修改部门信息
     * @param response
     * @param department
     * @return
     */
    @RequestMapping("/editDepartmentInfo")
    public boolean editDepartmentInfo(HttpServletResponse response,Ems4jDepartment department)
    {
//        department.setName(new String(department.getName().getBytes("iso-8859-1"),"utf-8"));
        department.setDepstatus((short)1);
        int i = departmentService.updateDepartment(department);
        if (i == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}
